#!/usr/bin/env python 
# -*- coding:utf-8 -*-

import json

import requests

from utils.log import log


class api(object):
    # 添加头信息。模拟浏览器(成员变量)
    __base_header = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36',
        'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'Cookie': '__utm_source=pinzhuan; __utm_medium=baidu; ZIROOM_PHONE=9; CURRENT_CITY_NAME=%E5%8C%97%E4%BA%AC; gr_user_id=85ab0eff-acdb-4a67-b6cd-d1593d9f0c21; PHPSESSID=uuorf2beqd0e8n47c2tpehrah6; Hm_lvt_038002b56790c097b74c818a80e3a68e=1542121503; BJ_nlist=%7B%2261667477%22%3A%7B%22id%22%3A%2261667477%22%2C%22sell_price%22%3A2690%2C%22title%22%3A%22%5Cu4e30%5Cu53f0%5Cu89d2%5Cu95e810%5Cu53f7%5Cu7ebf%5Cu89d2%5Cu95e8%5Cu4e1c%5Cu897f%5Cu9a6c%5Cu91d1%5Cu6da6%5Cu4e8c%5Cu533a3%5Cu5c45%5Cu5ba4-%5Cu5357%5Cu5367%22%2C%22add_time%22%3A1542122501%2C%22usage_area%22%3A17.2%2C%22floor%22%3A%225%22%2C%22floor_total%22%3A%226%22%2C%22room_photo%22%3A%22g2m1%5C%2FM00%5C%2F11%5C%2F1A%5C%2FChAFBlvGAQGASKleAAS0BNKH31I745.JPG%22%2C%22city_name%22%3A%22%5Cu5317%5Cu4eac%22%7D%2C%2261833426%22%3A%7B%22id%22%3A%2261833426%22%2C%22sell_price%22%3A2690%2C%22title%22%3A%22%5Cu4e30%5Cu53f0%5Cu9a6c%5Cu5bb6%5Cu58214%5Cu53f7%5Cu7ebf%5Cu516c%5Cu76ca%5Cu897f%5Cu6865%5Cu5bcc%5Cu5353%5Cu82d14%5Cu5c45%5Cu5ba4-%5Cu897f%5Cu5367%22%2C%22add_time%22%3A1542122418%2C%22usage_area%22%3A14.3%2C%22floor%22%3A%228%22%2C%22floor_total%22%3A%2220%22%2C%22room_photo%22%3A%22g2m1%5C%2FM00%5C%2F23%5C%2F87%5C%2FChAFB1vMYh6AQAr3AAei5vcIUJA593.JPG%22%2C%22city_name%22%3A%22%5Cu5317%5Cu4eac%22%7D%2C%2261770311%22%3A%7B%22id%22%3A%2261770311%22%2C%22sell_price%22%3A2690%2C%22title%22%3A%22%5Cu4e30%5Cu53f0%5Cu89d2%5Cu95e810%5Cu53f7%5Cu7ebf%5Cu89d2%5Cu95e8%5Cu4e1c%5Cu89d2%5Cu95e8%5Cu4e1c%5Cu91cc3%5Cu5c45%5Cu5ba4-%5Cu5317%5Cu5367%22%2C%22add_time%22%3A1542122343%2C%22usage_area%22%3A14.8%2C%22floor%22%3A%224%22%2C%22floor_total%22%3A%2219%22%2C%22room_photo%22%3A%22g2m1%5C%2FM00%5C%2F20%5C%2F46%5C%2FChAFB1vLSESAEWlaAATgttS1HWw947.JPG%22%2C%22city_name%22%3A%22%5Cu5317%5Cu4eac%22%7D%2C%2260482893%22%3A%7B%22id%22%3A%2260482893%22%2C%22sell_price%22%3A3330%2C%22title%22%3A%22%5Cu4e30%5Cu53f0%5Cu9a6c%5Cu5bb6%5Cu582110%5Cu53f7%5Cu7ebf%5Cu89d2%5Cu95e8%5Cu4e1c%5Cu6021%5Cu7136%5Cu5bb6%5Cu56ed6%5Cu53f7%5Cu96623%5Cu5c45%5Cu5ba4-%5Cu5357%5Cu5367%22%2C%22add_time%22%3A1542122259%2C%22usage_area%22%3A19.5%2C%22floor%22%3A%226%22%2C%22floor_total%22%3A%2211%22%2C%22room_photo%22%3A%22g2m1%5C%2FM00%5C%2F52%5C%2F77%5C%2FChAFB1vdg4CAcXBMAANc2nCQTy0387.JPG%22%2C%22city_name%22%3A%22%5Cu5317%5Cu4eac%22%7D%2C%2260286595%22%3A%7B%22id%22%3A%2260286595%22%2C%22sell_price%22%3A2390%2C%22title%22%3A%22%5Cu4e30%5Cu53f0%5Cu89d2%5Cu95e810%5Cu53f7%5Cu7ebf%5Cu5927%5Cu7ea2%5Cu95e8%5Cu946b%5Cu798f%5Cu91cc%5Cu5c0f%5Cu533a3%5Cu5c45%5Cu5ba4-%5Cu5357%5Cu5367%22%2C%22add_time%22%3A1542122171%2C%22usage_area%22%3A12.9%2C%22floor%22%3A%225%22%2C%22floor_total%22%3A%226%22%2C%22room_photo%22%3A%22g2m1%5C%2FM00%5C%2F60%5C%2FCE%5C%2FChAFB1viwk-AHcnAAAO1C6-Z0lg907.JPG%22%2C%22city_name%22%3A%22%5Cu5317%5Cu4eac%22%7D%2C%2260062539%22%3A%7B%22id%22%3A%2260062539%22%2C%22sell_price%22%3A1470%2C%22title%22%3A%22%5Cu4e30%5Cu53f0%5Cu65e7%5Cu5bab%5Cu6709%5Cu5fb7%5Cu5bb6%5Cu82d15%5Cu53f7%5Cu96623%5Cu5c45%5Cu5ba4-%5Cu5357%5Cu5367%22%2C%22add_time%22%3A1542121871%2C%22usage_area%22%3A10.84%2C%22floor%22%3A%227%22%2C%22floor_total%22%3A%229%22%2C%22room_photo%22%3A%22g2m1%5C%2FM00%5C%2F17%5C%2F4B%5C%2FChAFB1vIZ4eARS2yAAut-uja1XI355.JPG%22%2C%22city_name%22%3A%22%5Cu5317%5Cu4eac%22%7D%2C%2261775633%22%3A%7B%22id%22%3A%2261775633%22%2C%22sell_price%22%3A2690%2C%22title%22%3A%22%5Cu5927%5Cu5174%5Cu897f%5Cu7ea2%5Cu95e84%5Cu53f7%5Cu7ebf%5Cu897f%5Cu7ea2%5Cu95e8%5Cu745e%5Cu6d77%5Cu5bb6%5Cu56ed%5Cu4e94%5Cu533a3%5Cu5c45%5Cu5ba4-%5Cu5317%5Cu5367%22%2C%22add_time%22%3A1542121737%2C%22usage_area%22%3A18.5%2C%22floor%22%3A%223%22%2C%22floor_total%22%3A%226%22%2C%22room_photo%22%3A%22g2m1%5C%2FM00%5C%2FBA%5C%2F3A%5C%2FChAFB1ukqaSAckCEAAO-pX85Ggc419.jpg%22%2C%22city_name%22%3A%22%5Cu5317%5Cu4eac%22%7D%2C%2261775632%22%3A%7B%22id%22%3A%2261775632%22%2C%22sell_price%22%3A2230%2C%22title%22%3A%22%5Cu5927%5Cu5174%5Cu897f%5Cu7ea2%5Cu95e84%5Cu53f7%5Cu7ebf%5Cu897f%5Cu7ea2%5Cu95e8%5Cu745e%5Cu6d77%5Cu5bb6%5Cu56ed%5Cu4e94%5Cu533a3%5Cu5c45%5Cu5ba4-%5Cu5357%5Cu5367%22%2C%22add_time%22%3A1542121550%2C%22usage_area%22%3A14.2%2C%22floor%22%3A%223%22%2C%22floor_total%22%3A%226%22%2C%22room_photo%22%3A%22g2m1%5C%2FM00%5C%2FBB%5C%2F65%5C%2FChAFBlukqW6AB8CoAASl_JkCCVk281.jpg%22%2C%22city_name%22%3A%22%5Cu5317%5Cu4eac%22%7D%7D; Hm_lpvt_038002b56790c097b74c818a80e3a68e=1542122502; CURRENT_CITY_CODE=110000; mapType=%20',
        'Host': 'www.ziroom.com',
        'Referer': 'http://www.ziroom.com/map/',
        'Connection': 'keep-alive'
    }
    __log = log.getLog('zr')

    # get 请求
    def __sendGet(self, url, header=__base_header, count=3):
        response = None
        try:
            response = requests.get(url, headers=header)
            if response and response.status_code == 200:
                return response.content
            raise Exception(response.status_code)
        except Exception as e:
            self.__log.error("出现异常：%s" % e)
            self.__log.error('请求重试 code : %d' % (response.status_code if response else -1))
            if count > 0:
                time.sleep(1)
                count -= 1
                return self.__sendGet(url, header, count)
            else:
                return None
        finally:
            if response:
                response.close()

    # 过滤参数接口
    def getFilter(self):
        #
        res = self.__sendGet("http://www.ziroom.com/room/filter", self.__base_header)
        if res:
            json_res = json.loads(res)
            self.__log.debug("json_res = {}".format(json_res))
            if json_res['code'] == 200:
                return json_res['data']
            else:
                return None
        else:
            self.__log.error("---- 接口请求异常 ----")
            return None

    # 地区数量统计接口
    def getCountList(self, min_lng, max_lng, min_lat, max_lat, clng, clat, zoom):
        url = "http://www.ziroom.com/map/room/count?min_lng={}&max_lng={}&min_lat={}&max_lat={}&clng={}&clat={}&zoom={}".format(
            min_lng, max_lng, min_lat, max_lat, clng, clat, zoom)
        self.__log.debug(">>> request url = {}".format(url))
        res = self.__sendGet(url, self.__base_header)
        if res:
            json_res = json.loads(res)
            self.__log.debug("json_res = {}".format(json_res))
            if json_res['code'] == 200:
                return json_res['data']
            else:
                return None
        else:
            self.__log.error("---- 接口请求异常 ----")
            return None

    # 地图房屋列表接口
    def getList(self, min_lng, max_lng, min_lat, max_lat, clng, clat, zoom, p, resblock_id = None):
        url = "http://www.ziroom.com/map/room/list?min_lng={}&max_lng={}&min_lat={}&max_lat={}&clng={}&clat={}&zoom={}&p={}".format(
            min_lng, max_lng, min_lat, max_lat, clng, clat, zoom, p)
        if resblock_id is not None:
            url += "&resblock_id={}".format(resblock_id)
        self.__log.debug(">>> request url = {}".format(url))
        res = self.__sendGet(url, self.__base_header)
        if res:
            json_res = json.loads(res)
            self.__log.debug("json_res = {}".format(json_res))
            if json_res['code'] == 200:
                return json_res['data']
            else:
                return None
        else:
            self.__log.error("---- 接口请求异常 ----")
            return None


if __name__ == "__main__":
    print("start ...")
    # 全局的信息
    # min_lng = 116.174609
    # max_lng = 116.633391
    # min_lat = 39.830608
    # max_lat = 39.999287
    # clng = 116.404
    # clat = 39.915
    # zoom = 12
    # p = 1
    # 丰台的信息
    # min_lng=116.240543
    # max_lng=116.308527
    # min_lat=39.818809
    # max_lat=39.894137
    # clng=116.274535
    # clat=39.856483
    # zoom=14
    # p=1
    # model1
    min_lng=116.088659
    max_lng=116.362319
    min_lat=39.720587
    max_lat=40.095613
    clng=116.225489
    clat=39.908359
    zoom=12
    p=1

    api = api()

    # api.getFilter()
    data2 = api.getCountList(min_lng, max_lng, min_lat, max_lat, clng, clat, zoom)
    #api.getList(min_lng, max_lng, min_lat, max_lat, clng, clat, zoom, p)

    print("data2 = {}".format(data2))

    # data2['aaa'] = 'aaa111'
    # print("data2 = {}".format(data2[0]))
    #
    # dict1 = {"aaa":111, "bbb":"222"}
    # print("dict1 = {}".format(dict1))
    # dict1["ccc"] = "ccc1"
    # print("dict1 = {}".format(dict1))
